<?php

class Accounts extends CI_Controller {

	function __construct(){
	
		parent::__construct();
		$this->load->model('companies_m');
		$this->load->model('office_services_m');
		$this->load->model('office_m');
		$this->load->model('categories_m');
		$this->load->model('user');
		$this->_check_session();
	}
	function _check_session(){
		
		if($this->session->userdata('username') == false){
			redirect('login');
		}
		
	}
	function logout(){
		$this->session->sess_destroy();
		redirect('login');
	}
	function index(){
		$user = $this->user->get_user_info();
		$company = $this->companies_m->get_companies_by_user($user->id);
		$office = $this->office_m->get_office_by_client($user->id);
		$office_services = $this->office_services_m->get_office_by_user($user->id);
		$categories = $this->categories_m->display_all();
		$data = array(
			'title' => 'Welcome | '.$this->session->userdata('username'),
			'company' => $company, // if false means company not added yet..
			'office_services' => $office_services, // if false means no offices yet..
			'$office' => $office, // if false means no office added yet..
			'categories' => $categories // if false means its blank..
		);
		$this->load->view('account/includes/header',$data);
		if($company == false){
			$this->load->view('account/reg_cont',$data);
		}
		else if($office_services == false){
			$this->load->view('account/reg_office_services',$data);
		}
		else if($office == false){
			$this->load->view('account/reg_office',$data);
		}
		else{
			$this->load->view('account/home',$data);
		}
		$this->load->view('account/includes/jscript.php');
		$this->load->view('account/includes/footer');
	} // end index..
	
	function addcompany(){ // adding new company..
		
		$res = $this->companies_m->add_company();
		
		if($res) redirect('accounts'); else var_dump(mysql_error());
	}// end adding new company..

	function add_office(){
		
		$res = $this->office_m->add_office_m();
		if($res) redirect('accounts'); else var_dump(mysql_error());
		
	}
	/* profile */
	function profile(){
		
		$page = $this->uri->segment(2);
		$action = $this->uri->segment(3);
		$user = $this->user->get_user_info();
		$data = array(
			'title' => 'Welcome | '.$this->session->userdata('username'),
			'user' => $user
		);
		
		if($page == 'profile' && $action == 'edit'){
			$res = $this->user->update();
			if($res) redirect('accounts/profile');
		}
		
		$this->load->view('account/includes/header',$data);
		
		if($page == 'profile'){
			$this->load->view('account/profile/view.php',$data);
		}
		else{
			$this->load->view('account/profile/view.php',$data);
		}
		$this->load->view('account/includes/footer',$data);
		
		
		
	}// end profile function
}// end accounts class.. 